from manim import *
class math(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text0 = Tex(r"\text{设}$\varphi \left( x\right) $\text{为连续函数,}$\left| \varphi \left( x\right) \right| \leq k$\text{(k为常数),求微分方程}"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,2.5,0])
        text00 = Tex(r"$\dfrac{dy}{dx}+y=\varphi \left( x\right) $\text{满足}"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([27,2.5,0])
        text1 = Tex(r"\text{初始条件}$y\left( 0\right) =0$\text{为连续函数,}$y\left( x\right) $\text{,并证明当}$x\geq 0$\text{时,有}"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-13,-2.5,0])
        text01 = Tex(r"$\left| y\left( x\right) \right| \leq k\left( 1-e^{-x}\right) $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([31,-2.5,0])
        
        self.play(Write(text0),Write(text1),Write(text00),Write(text01),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text0.animate().move_to([-15,20,0]) ,text00.animate().move_to([27,20,0]) ,text1.animate().move_to([-13,15,0]),text01.animate().move_to([31,15,0]) )
        
       

        long_text=("一阶线性非齐次方程")
        text100 = Text(long_text,color=YELLOW,
                   font_size=200).move_to([25,25,0])
        #框框标
        rectangle1=Rectangle(color="#00FF00",fill_opacity=0.3,width=18,height=6
                            ,stroke_color="#00FF00",stroke_width=20)
        rectangle1.move_to([24,20,0])
        self.play(Create(rectangle1),Create(text100))
        self.wait(0.2)
        text2 = Tex(r"$y=e^{-x}\cdot \left[ \int \varphi \left( x\right) e^{x}dx+C\right] $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,10,0])
        self.play(ReplacementTransform(rectangle1,text2),FadeOut(text100))
        
        #下划线
        dot1=Dot([-29,7,0])
        dot2=Dot([-16,7,0])
        line=Line(dot1,dot2).set_color("#00FF00")
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))
        self.wait(0.2)
        text3 = Tex(r"$y=e^{-x}\cdot \left[ \int _{0}^{x}\varphi \left( t\right) e^{t}dt+C\right] $"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,10,0])
        self.play(ReplacementTransform(text2,text3))
        rectangle2=Rectangle(color="#00F3FF",fill_opacity=0.3,width=10,height=5
                            ,stroke_color="#00F3FF",stroke_width=20)
        rectangle2.move_to([-30,15,0])
        self.play(Create(rectangle2))
        self.wait(0.2)
        text4 = Tex(r"$y\left( 0\right) =0\Rightarrow C=0$"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,5,0])
        self.play(ReplacementTransform(rectangle2,text4))
        text5 = Tex(r"$y\left( x\right) =e^{-x}\cdot \int _{0}^{x}\varphi \left( t\right) e^{t}dt$"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,0,0])
        self.play(Write(text5))
        rectangle3=Rectangle(color="#00F3FF",fill_opacity=0.3,width=22,height=5
                            ,stroke_color="#00F3FF",stroke_width=20)
        rectangle3.move_to([31,15,0])
        self.play(Create(rectangle3))
        self.wait(0.2)
        text6 = Tex(r"\text{即证：}$\left| e^{-x}\cdot \int _{0}^{x}\varphi \left( t\right) e^{t}dt\right| \leq k\left( 1-e^{-x}\right) $",color="#FFFF00"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,10,0])
        self.play(ReplacementTransform(rectangle3,text6))
        text7 = Tex(r"$ e^{-x}\cdot \left|\int _{0}^{x}\varphi \left( t\right) e^{t}dt\right| $",color="#00FF00"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-7,0])
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.3,width=6,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle4.move_to([7.5,10,0])
        self.play(Create(rectangle4))
        self.wait(0.2)
        self.play(ReplacementTransform(rectangle4,text7))
        text8 = Tex(r"\text{不等式1：}$ \left| a\pm b\right| \leq \left| a\right| +\left| b\right| $",color="#FFFF00"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-13,0])
        text9 = Tex(r"\text{不等式2：}$ \left| \int _{a}^{b}f\left( x\right) dx\right| \leq \int _{a}^{b}\left| f\left( x\right) \right| dx$",color="#FFFF00"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-19,0])
        self.play(Write(text8),Write(text9))
        text10 = Tex(r"$\leq e^{-x}\cdot \int _{0}^{x}\left| \varphi \left( x\right) \right| e^{t}dt$",color=WHITE
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text7,RIGHT,buff=1)
        self.play(ReplacementTransform(text9,text10),FadeOut(text8))


        text11 = Tex(r"$\leq ke^{-x}\cdot \int _{0}^{x} e^{t}dt$",color=WHITE
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text10,RIGHT,buff=1)
        rectangle5=Rectangle(color="#00F3FF",fill_opacity=0.3,width=15,height=5
                            ,stroke_color="#00F3FF",stroke_width=20)
        rectangle5.move_to([-17.5,20,0])
        self.play(Create(rectangle5))
        self.wait(0.2)
        self.play(ReplacementTransform(rectangle5,text11))

        
        rectangle6=Rectangle(color="#00F3FF",fill_opacity=0.3,width=10,height=5
                            ,stroke_color="#00F3FF",stroke_width=20)
        rectangle6.move_to([15,-7,0])
        text12 = Tex(r"$e^{x}-1$",color="#FFFF00"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle6,DOWN,buff=1)
        self.play(Create(rectangle6))
        self.play(Write(text12))
        text13 = Tex(r"$=k\left( 1-e^{-x}\right) $",color="#00FF00"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle6,RIGHT,buff=1)
        
        self.play(Write(text13))
        
        


